package yuyicman.core.factory
{
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import yuyicman.core.factory.AssetFactory;
	import yuyicman.debug.Debugger;
	
	[Event(name="event_asset_factory_ready", type="yuyicman.core.factory.AssetFactory")]
	
	public class Resource extends EventDispatcher
	{
		
		private var _af:AssetFactory;
		private static var _instance:Resource;
		
		public function Resource( singleton:Singleton ){
		}
		
		public function init( bytes:Class ):Resource {
			
			_af = new AssetFactory();
			_af.addEventListener( AssetFactory.EVENT_ASSET_FACTORY_READY, _assetFactoryReady );
			_af.initialize( bytes );
			return this;
			
		}
		
		private function _assetFactoryReady( e:Event ):void{
			
			_af.removeEventListener(AssetFactory.EVENT_ASSET_FACTORY_READY, _assetFactoryReady);
			dispatchEvent( e );
			
		}
		
		public static function createObject( name:String ):*{
			return instance._af.createObject(name);
		}
		
		public static function createDisplayObject( name:String ):DisplayObject {
			return createObject( name ) as DisplayObject;
		}
		
		public static function createBitmapData( name:String ):BitmapData {
			return instance._af.createBitmapData(name);
		}
		
		private static function get instance():Resource {
			if ( !_instance ) {
				_instance = new Resource( new Singleton );
			}
			return _instance;
		}
		
		public static function initWithBytes( bytes:Class ):Resource {
			return instance.init( bytes );
		}
		
	}
}

internal class Singleton{}